package com.holly.common_view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.holly.common.utils.L;

/**
 * 2016/11/25 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */

public class PageFlitView extends LinearLayout {
    public static final int DEFAULT_HEIGHT = 40;
    public static final int DEFAULT_WIDTH = 600;
    public static final int DEFAULT_STROKE_COLOR = Color.parseColor("#5DD5D4");
    public static final int DEFAULT_TEXT_COLOR = Color.parseColor("#1976d2");
    private final String TAG = L.createTag(this);
    private Button mBtFirstPage;
    private Button mBtPreviousPage;
    private TextView mTvPage;
    private Button mBtNextPage;
    private Button mBtLastPage;
    private OnPageChangedListener mOnPageChangedListener;
    private int mPageCount = 0;
    private int mPageIndex = 0;
    private ViewPager mUp;

    public PageFlitView(Context context) {
        this(context, null);
    }

    public PageFlitView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PageFlitView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        initView();
        initView1();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(DEFAULT_WIDTH + getPaddingLeft() + getPaddingRight(), MeasureSpec.EXACTLY);
        }
        if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(DEFAULT_HEIGHT + getPaddingTop() + getPaddingBottom(), MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void setPageCount(int pageCount) {
        L.d(TAG, "setPageCount: pageCount = " + pageCount);
        if (pageCount < 0) {
            throw new IllegalArgumentException("pageCount 不能小于0");
        }
        int previousCount = mPageCount;
        mPageCount = pageCount;
        if (pageCount == 0) {
            mPageIndex = 0;
            mTvPage.setText("0/0");
        } else {
            if (mPageIndex <= 0) {
                mPageIndex = 1;
            } else if (mPageIndex > mPageCount) {
                mPageIndex = mPageCount;
            }
            mTvPage.setText(mPageIndex + "/" + pageCount);
        }
        if (mOnPageChangedListener != null && mPageCount != previousCount) {
            mOnPageChangedListener.onPageCountChanged(mPageCount, previousCount);
        }
    }

    public void setOnPageChangedListener(OnPageChangedListener onPageChangedListener) {
        mOnPageChangedListener = onPageChangedListener;
    }

    private void initView1() {
        GradientDrawable mBackgroundDrawable = new GradientDrawable();
        mBackgroundDrawable.setStroke(1, DEFAULT_STROKE_COLOR);
        mBackgroundDrawable.setColor(Color.WHITE);
        mBtFirstPage = new Button(getContext());
        mBtPreviousPage = new Button(getContext());
        mBtNextPage = new Button(getContext());
        mBtLastPage = new Button(getContext());
        mTvPage = new TextView(getContext());
        mBtFirstPage.setTextColor(DEFAULT_TEXT_COLOR);
        mBtPreviousPage.setTextColor(DEFAULT_TEXT_COLOR);
        mBtNextPage.setTextColor(DEFAULT_TEXT_COLOR);
        mBtLastPage.setTextColor(DEFAULT_TEXT_COLOR);
        mTvPage.setTextColor(DEFAULT_TEXT_COLOR);
        mBtFirstPage.setText("首页");
        mBtPreviousPage.setText("上一页");
        mBtNextPage.setText("下一页");
        mBtLastPage.setText("尾页");
        mTvPage.setText("0/0");
        mBtFirstPage.setGravity(Gravity.CENTER);
        mBtPreviousPage.setGravity(Gravity.CENTER);
        mBtNextPage.setGravity(Gravity.CENTER);
        mBtLastPage.setGravity(Gravity.CENTER);
        mTvPage.setGravity(Gravity.CENTER);
        mBtFirstPage.setBackground(mBackgroundDrawable);
        mBtPreviousPage.setBackground(mBackgroundDrawable);
        mBtNextPage.setBackground(mBackgroundDrawable);
        mBtLastPage.setBackground(mBackgroundDrawable);
        mTvPage.setBackground(mBackgroundDrawable);
        LinearLayout.LayoutParams endParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
        endParams.weight = 1;
        endParams.setMargins(0, 0, 0, 0);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
        params.weight = 1;
        params.setMargins(0, 0, 5, 0);
        addView(mBtFirstPage, params);
        addView(mBtPreviousPage, params);
        addView(mTvPage, params);
        addView(mBtNextPage, params);
        addView(mBtLastPage, endParams);
        initListener();
    }

    private void initListener() {
        mBtFirstPage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPageIndex > 1) {
                    int previous = mPageIndex;
                    mPageIndex = 1;
                    onPageLabelChanged(mPageIndex, mPageCount);
                    if (mOnPageChangedListener != null) {
                        mOnPageChangedListener.onPageChanged(mPageIndex, previous, mPageCount);
                    }
                }
            }
        });

        mBtLastPage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPageIndex < mPageCount) {
                    int previous = mPageIndex;
                    mPageIndex = mPageCount;
                    onPageLabelChanged(mPageIndex, mPageCount);
                    if (mOnPageChangedListener != null) {
                        mOnPageChangedListener.onPageChanged(mPageIndex, previous, mPageCount);
                    }
                }
            }
        });

        mBtPreviousPage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPageIndex > 1) {
                    int previous = mPageIndex;
                    mPageIndex = mPageIndex - 1;
                    onPageLabelChanged(mPageIndex, mPageCount);
                    if (mOnPageChangedListener != null) {
                        mOnPageChangedListener.onPageChanged(mPageIndex, previous, mPageCount);
                    }
                }
            }
        });

        mBtNextPage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPageIndex < mPageCount) {
                    int previous = mPageIndex;
                    mPageIndex = mPageIndex + 1;
                    onPageLabelChanged(mPageIndex, mPageCount);
                    if (mOnPageChangedListener != null) {
                        mOnPageChangedListener.onPageChanged(mPageIndex, previous, mPageCount);
                    }
                }
            }
        });
    }

    private void onPageLabelChanged(int pageIndex, int pageCount) {
        mTvPage.setText(pageIndex + "/" + pageCount);
    }

    public void setPageIndex(int pageIndex) {
        mPageIndex = pageIndex;
        mTvPage.setText(mPageIndex + "/" + mPageCount);
    }

    public int getPageIndex() {
        return mPageIndex;
    }

    public interface OnPageChangedListener {
        void onPageChanged(int nowPage, int previousPage, int pageCount);

        void onPageCountChanged(int nowCount, int previousCount);
    }
}
